import re
def JJCCF(string): # 核心算法加减乘除
    string = re.sub("--", "+", string, 1)
    string = re.sub("\+-", "-", string, 1)
    result = re.search("\d+\.?\d*[*/]-?\d+\.?\d*",string)  # 提取乘除，返回对象 or None
    if result:
        rea = re.findall("-?\d+\.?\d*", result.group())  # 提取需要运算的数据
        if "*" in result.group():return JJCCF(re.sub("\d+\.?\d*[*/]-?\d+\.?\d*", str(float(rea[0])*float(rea[1])), string, 1))
        elif "/" in result.group():return JJCCF(re.sub("\d+\.?\d*[*/]-?\d+\.?\d*", str(float(rea[0])/float(rea[1])), string, 1))
    else:  # 计算加减
        result = re.search("-?\d+\.?\d*[-+]-?\d+\.?\d*", string)  # 提取加减
        if result:
            rea = re.findall("-?\d+\.?\d*", result.group())  # 提取需要运算的数据,只有加法（减法也转换换加法）
            return JJCCF(re.sub("-?\d+\.?\d*[-+]-?\d+\.?\d*", str(float(rea[0]) + float(rea[1])), string, 1))
        else:return string
def calculator(string):
    if re.search("(\((?=((?!\().)*$))(.*?\))", re.subn(" ", "", string)[0]):  # 提取括号（2/2+2/1）,顺便去空格
        temp = (re.search("[^\(].*[^\)]", re.search("(\((?=((?!\().)*$))(.*?\))", re.subn(" ", "", string)[0]).group()).group())  # 去除括号
        return calculator(re.sub("(\((?=((?!\().)*$))(.*?\))", JJCCF(temp), string, 1))  # 加减乘除运算，替换内容（包含括号）
    else:return JJCCF(string)


if __name__ == '__main__':
    string = "1-2*((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))"
    # string = "((60-30+(-40/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))"
    # string = "1-2*-1388338.2476190478"
    # string = "(-1)/2+1"
    # string = "(9-2*5/3+7/3*99/4*2998+10*568/14)"
    # string = "(9-2*5/3+7/3*99/4*2998+10*568/14)--1+1-2+3+-1"
    # string = "-1+1/2-2"
    # string = "25*64+(84*24/6+7*(25/6+45-25*68)/2)+-358-21"
    print("ev:", eval(string))
    print("me:", calculator(string))

    # 计算器

    # 实现能计算类似
    # 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等类似公式的计算器程序


